<?php
declare(strict_types=1);

namespace app\common\job;

use app\common\model\banxi\word\WordDictionary;
use app\common\service\queue\QueueService;
use app\common\service\word\WordService;
use think\facade\Log;
use think\queue\Job;

set_time_limit(0);
class SayWordRunner
{
    public function fire(Job $job, array $data): void
    {

        logSave([
            'file' => __FILE__,
            'message' => 'SayWordRunner Exec',
            'data' => $data,
            'start_time' => date('Y-m-d H:i:s')
        ], 'sayWordRunner');
        try {
            $word = $data['word_data'];
            // 使用容器解析服务实例
            $wordService = app()->make(WordService::class);
            $result = $wordService->sayWordCompositing($word);
            if ($result) {
                // 任务执行成功，删除任务
                $word = new QueueService();
                $word->addSayWordVocalJob();
                // 任务执行成功，删除任务
                $job->delete();
                logSave([
                    'file' => __FILE__,
                    'message' => "SayWordRunner finished",
                    'data' => $data,
                    'end_time' => date('Y-m-d H:i:s')
                ], 'sayWordRunner');
            } else {
                // 任务执行失败，处理失败情况
                $this->handleFailure($job, $data);
            }
        } catch (\Exception $e) {
            // 捕获异常并处理失败情况
            $this->handleFailure($job, $data);
            logSave([
                'file' => __FILE__,
                'message' => "SayWordRunner Exception",
                'data' => $data,
                'error' => $e->getMessage(),
                'trace' => $e->getTraceAsString()
            ], 'sayWordRunner');
        } catch (\Throwable $t) {
            // 捕获致命错误
            $this->handleFailure($job, $data);
            logSave([
                'file' => __FILE__,
                'message' => "SayWordRunner Throwable",
                'data' => $data,
                'error' => $t->getMessage(),
                'trace' => $t->getTraceAsString()
            ], 'sayWordRunner');
        }
    }

    /**
     * 处理任务失败情况
     */
    private function handleFailure(Job $job, array $data): void
    {
        $attempts = $job->attempts();

        // 如果重试次数超过3次，则删除任务并记录失败
        if ($attempts > 3) {
            $job->delete();
            logSave([
                'file' => __FILE__,
                'message' => "队列任务失败超过3次，已删除",
                'data' => $data,
                'attempts' => $attempts,
                'end_time' => date('Y-m-d H:i:s')
            ], 'sayWordRunner');
        } else {
            logSave([
                'file' => __FILE__,
                'message' => "队列任务失败，将自动重试",
                'data' => $data,
                'attempts' => $attempts,
            ], 'sayWordRunner');
            // 任务会自动重新加入队列
        }
    }

    /**
     * 默认任务处理
     */
    private function processDefault(array $data): bool
    {
        // 实现默认处理逻辑
        return true;
    }

    /**
     * 任务失败回调
     */
    public function failed(array $data): void
    {
        // 记录失败任务日志
        Log::error('队列任务最终失败', $data);
    }
}
